package com.dongdong.zhuangji.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongdong.zhuangji.controller.utils.R;
import com.dongdong.zhuangji.entity.ControlPoints;
import com.dongdong.zhuangji.service.ControlPointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/controlPoints")
public class ControlPointsController {

    @Autowired
    private ControlPointsService controlPointsService;

    // 添加控制点
    @PostMapping
    public R addControlPoint(@RequestBody ControlPoints controlPoints) {
        return new R(controlPointsService.save(controlPoints));
    }

    // 查询所有控制点
    @GetMapping
    public R getAllControlPoints() {
        List<ControlPoints> list=controlPointsService.list();
        return new R(list);
    }

    // 根据 ID 查询控制点
    @GetMapping("/{id}")
    public R getControlPointById(@PathVariable Integer id) {
        return new R(controlPointsService.getById(id));
    }

    // 根据 ID 更新控制点
    @PutMapping("/{id}")
    public R updateControlPoint(@PathVariable Integer id, @RequestBody ControlPoints controlPoints) {
        controlPoints.setId(id);
        return new R(controlPointsService.updateById(controlPoints));
    }

    // 根据 ID 删除控制点
    @DeleteMapping("/{id}")
    public R deleteControlPoint(@PathVariable Integer id) {
        return new R(controlPointsService.removeById(id)) ;
    }

    // 条件分页查询接口
    @GetMapping("/page")
    public R pageQuery(@RequestParam(defaultValue = "1") long current,
                                          @RequestParam(defaultValue = "10") long pageSize,
                                          ControlPoints controlPoints) {

       // System.out.println(current+" "+pageSize+" "+controlPoints.toString());
        Page<ControlPoints> page = new Page<>(current, pageSize);

        Page<ControlPoints> page1=controlPointsService.pageQuery(page, controlPoints);
        if (current>page1.getPages()){
            page.setCurrent(page1.getPages());
            page1=(controlPointsService.pageQuery(page, controlPoints));
        }
        return  new R(page1);
    }
}    